package com.bawei.demo_shoppingcar.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.bawei.demo_shoppingcar.R;
import com.bawei.demo_shoppingcar.activity.MainActivity;
import com.bawei.demo_shoppingcar.application.ContentData;
import com.bawei.demo_shoppingcar.bean.ShoppingDBbean;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;

/**
 * Created by HP on 2017/3/22.
 */

public class CarDBAdapter extends BaseAdapter {

    private final Context context;
    private final ArrayList<ShoppingDBbean> listData;

    public CarDBAdapter(Context context, ArrayList<ShoppingDBbean> listData) {
        this.context = context;
        this.listData = listData;
    }

    @Override
    public int getCount() {
        return listData.size();
    }

    @Override
    public Object getItem(int position) {
        return listData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null){
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.shopping_lv_item,null);
            viewHolder.imageView = (ImageView) convertView.findViewById(R.id.shopping_item_image);
            viewHolder.name = (TextView) convertView.findViewById(R.id.shopping_item_name);
            viewHolder.price = (TextView) convertView.findViewById(R.id.shopping_item_price);
            viewHolder.num = (TextView) convertView.findViewById(R.id.shopping_item_num);
            viewHolder.checkBox = (CheckBox) convertView.findViewById(R.id.shopping_item_check);
            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.name.setText(listData.get(position).getShopping_name());
        viewHolder.price.setText("单价:"+listData.get(position).getShopping_price()+"元");
        viewHolder.num.setText("购买数量:"+listData.get(position).getNum()+"个");
        ImageLoader.getInstance().displayImage(listData.get(position).getShopping_image(),viewHolder.imageView, ContentData.options);
        viewHolder.checkBox.setChecked(listData.get(position).isPitch());
        viewHolder.checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listData.get(position).setPitch(!listData.get(position).isPitch());

            }
        });



        return convertView;
    }

    class ViewHolder {

        ImageView imageView;
        TextView name,price,num;
        CheckBox checkBox;
    }


}
